const Goods = require('../model/goods.model')

class GoodsService {
 async createGoods(goods) {
  const res = await Goods.create(goods)
  return res.dataValues
 }
 async updateGoods(id, goods) {
  const res = await Goods.update(goods, { where: { id } })
  return !!res.at(0)
 }
 async removeGoods(id) {
  const res = await Goods.destroy({ where: { id } })
  return res
 }
 async restoreGoods(id) {
  const res = await Goods.restore({ where: { id } })
  return res
 }
 /**
  * 分页获取数据
  * @param {number} pageNum 当前页数
  * @param {number} pageSize 几条数据
  * @returns {pageNum,pageSize,total,list}
  */
 async findGoods(pageNum, pageSize) {
  const offset = (pageNum - 1) * pageSize
  const limit = pageSize * 1
  // 因为使用了偏执表模型所以会自动去掉哪些已经被标记删除的记录
  // const count = await Goods.count()
  // const rows = await Goods.findAll({ offset, limit })
  // 与上面的两句意思一样
  const { count, rows } = await Goods.findAndCountAll({ offset, limit })
  return {
   pageNum, pageSize,
   total: count,
   list: rows
  }
 }
 /**
  * 获取所有未删除的数据
  * @returns {}
  */
 async findAllGoods() {
  const rows = await Goods.findAll()
  return rows
 }
}

module.exports = new GoodsService()
